package kelisPkg.soundPackage
{
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundLoaderContext;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	
	public class SoundBase
	{
		private var _type:String;
		private var _eff:String;
		private var _sound:Sound;
		private var _soundChannel:SoundChannel;
		private var _time:Timer;
		public var vol:int=100;
		public function SoundBase(str:String,typex:String="once",eff:String="null")
		{
			_type=typex;
			_eff=eff;
			_sound=new Sound();
			_sound.addEventListener(Event.COMPLETE,_loadComplete);
			_sound.load(new URLRequest(str));
		}
		private function _loadComplete(e:Event):void
		{
			play(_type,_eff);
		}
		public function play(typex:String="once",eff:String="null",startTime:Number=0):void{
			_type=typex;
			_eff=eff;
			_soundChannel=_sound.play(startTime);
			_soundChannel.addEventListener(Event.SOUND_COMPLETE,_soundComplete);
			_time=new Timer(_sound.length);
			_time.addEventListener(TimerEvent.TIMER,_timeRun);
            switch(_eff){
				case SoundStatus.FADE_IN:
					_volume=0;
				case SoundStatus.FADE_OUT:
					_volume=1;
				case SoundStatus.FADE_INANDOUT:
					_volume=0;
					_time.start();
					break
				default :
					_time.removeEventListener(TimerEvent.TIMER,_timeRun);
					_time=null;
					break
			}
		}
		private function _timeRun(e:Event):void
		{
			if(_eff==SoundStatus.FADE_IN||_eff==SoundStatus.FADE_INANDOUT&&_time.currentCount<1000){
				_volume+=0.001;
			}else if(_eff==SoundStatus.FADE_IN||_eff==SoundStatus.FADE_INANDOUT&&(_sound.length-_time.currentCount)<1000){
				_volume-=0.001;
			}else{
				_volume=1;
			}
		}
		private function _soundComplete(e:Event):void
		{
			
			_time.removeEventListener(TimerEvent.TIMER,_timeRun);
			_time.stop();
			_time=null;
			_soundChannel.removeEventListener(Event.SOUND_COMPLETE,_soundComplete);
			if(_type==SoundStatus.LOOP){
				_soundChannel=_sound.play(0);
				_soundChannel.addEventListener(Event.SOUND_COMPLETE,_soundComplete);
			}else if(_type==SoundStatus.ONCE){
				_soundChannel.stop();
			}
		}
		
		public function replay():void{
			play(_type,_eff,_soundChannel.position);
		}
		public function pause():void{
			_soundChannel.stop();
		}
		private function set _volume(value:Number):void{
			_soundChannel.soundTransform.volume=value*vol/100;
		}
		private function get _volume():Number{
			return _soundChannel.soundTransform.volume
		}
		
	}
}